package com.bbm.communitysystem.controller;


import com.bbm.communitysystem.core.domain.R;
import com.bbm.communitysystem.domain.Menu;
import com.bbm.communitysystem.domain.bo.MenuBo;
import com.bbm.communitysystem.domain.dto.MenuDto;
import com.bbm.communitysystem.domain.vo.MenuVo;
import com.bbm.communitysystem.service.IMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bbm
 * @since 2025-02-18
 */
@Tag(name = "菜单")
@RestController
@RequestMapping("/menu")
@RequiredArgsConstructor
public class MenuController {

    private final IMenuService menuService;

    @Operation(summary = "删除菜单")
    @DeleteMapping("/{id}")
    @Parameter(description = "菜单id", name = "id")
    public R deleteMenu(@PathVariable("id") Integer id) {
        int i = menuService.DeleteMenu(id);

        if (i != 0) {
            return R.error("删除失败");
        }

        return R.ok("删除成功");
    }


    @Operation(summary = "修改菜单")
    @Parameter(description = "菜单id")
    @PutMapping("/")
    public R putMenu(@RequestBody MenuDto menuDto) {


        boolean update = menuService.lambdaUpdate()
                .set(!menuDto.getMenuName().isEmpty(), Menu::getMenuName, menuDto.getMenuName())
                .set(menuDto.getMenuWidth() != null, Menu::getMenuWidth, menuDto.getMenuWidth())
                .set(menuDto.getStatus() != null, Menu::getStatus, menuDto.getStatus())
                .set(!menuDto.getPageName().isEmpty(), Menu::getPageName, menuDto.getPageName())
                .set(!menuDto.getGoPage().isEmpty(), Menu::getGoPage, menuDto.getGoPage())

                .set(Menu::getUpdateTime, System.currentTimeMillis() + "")
                .set(Menu::getUpdateBy, menuDto.getUpdateBy())
                .eq(Menu::getId, menuDto.getId())
                .update();

        if (!update) {
            return R.error("修改失败");
        }

        return R.ok("修改成功");
    }

    @Operation(summary = "修改菜单图片")
    @PutMapping("/img")
    @Parameters({
            @Parameter(description = "用户id", name = "id"),
            @Parameter(description = "菜单id", name = "menuId"),
    })
    public R putMenuImg(@RequestParam("id") Integer userId, @RequestParam("img") MultipartFile file,
                        @RequestParam("menuId") Integer menuId) {

        MenuBo menuBo = new MenuBo();

        menuBo.setId(menuId);
        menuBo.setUpdateBy(userId + "");
        menuBo.setMenuImg(file);

        MenuDto menuDto = menuService.putMenuImg(menuBo);

        if (menuDto == null) {
            return R.error("修改失败");
        }

        return R.ok("修改成功");
    }

    @Operation(summary = "新增菜单")
    @PostMapping("/")
    @Parameters({
            @Parameter(description = "菜单文字", name = "text"),
            @Parameter(description = "属于哪个页面？", name = "pageName"),
            @Parameter(description = "跳转到哪个页面？", name = "goPage"),
            @Parameter(description = "权重", name = "width"),
            @Parameter(description = "用户id", name = "userId"),
    })
    public R<String> addMenu(
            @RequestParam("img") MultipartFile file, @RequestParam("text") String text,
            @RequestParam("width") Integer width, @RequestParam("pageName") String pageName,
            @RequestParam("userId") Integer userId, @RequestParam("goPage") String goPage
            ) {

        MenuBo menuBo = new MenuBo();

        menuBo.setMenuName(text);
        menuBo.setMenuWidth(width);
        menuBo.setUpdateBy(userId + "");
        menuBo.setStatus(true);
        menuBo.setMenuImg(file);
        menuBo.setPageName(pageName);
        menuBo.setGoPage(goPage);


        MenuDto menuDto = menuService.addMenu(menuBo);

        if (menuDto == null) {
            return R.error("新增失败");
        }

        return R.ok("新增成功");
    }

    @Operation(summary = "获取所有的菜单,开发环境使用")
    @GetMapping("/dev/list")
    public R getAllMenu() {
        return R.ok("获取成功", menuService.list());
    }

    @Operation(summary = "根据页面名称获取菜单")
    @GetMapping("/{pageName}")
    @Parameter(description = "页面名称", name = "pageName")
    public R<List<MenuVo>> getMenuGroup(@PathVariable("pageName") String pageName) {

        List<Menu> list = menuService.lambdaQuery()
                .eq(Menu::getStatus, 1)
                .eq(Menu::getPageName, pageName)
                .orderByDesc(Menu::getMenuWidth)
                .list();

        List<MenuVo> menuVos = new ArrayList<>();

        list.forEach(item -> {
            MenuVo menuVo = new MenuVo();
            menuVo.setId(item.getId());
            menuVo.setText(item.getMenuName());
            menuVo.setWidth(item.getMenuWidth());
            menuVo.setImg(item.getMenuImg());
            menuVo.setHref(item.getGoPage());
            menuVos.add(menuVo);
        });


        return R.ok("获取成功", menuVos);
    }


}
